package com.fishery.base.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.base.entity.Germchit;
import com.fishery.base.entity.GermchitManagement;
import com.fishery.base.entity.GermchitOrder;
import com.fishery.base.entity.vo.BaseGermchitAmountVo;
import com.fishery.base.entity.vo.BaseGermchitVo;
import com.fishery.base.entity.vo.GermchitOrderVo;
import com.fishery.base.mapper.GermchitManagementMapper;
import com.fishery.base.mapper.GermchitMapper;
import com.fishery.base.mapper.GermchitOrderMapper;
import com.fishery.base.service.GermchitManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ghost
 * @since 2021-01-15
 */
@Service
public class GermchitManagementServiceImpl extends ServiceImpl<GermchitManagementMapper, GermchitManagement> implements GermchitManagementService {

	@Autowired
	private GermchitMapper germchitMapper;

	@Autowired
	private GermchitManagementMapper germchitManagementMapper;

	@Autowired
	private GermchitOrderMapper germchitOrderMapper;

	@Autowired
	private ExecutorService threadPool;

	/**
	 * 购买种苗
	 *
	 * @param germchitOrderVo
	 * @return
	 */
	@Override
	@Transactional
	public boolean purchaseGermchit(GermchitOrderVo germchitOrderVo) {
		// 获取买家种苗信息
		Germchit germchit = germchitMapper.selectById(germchitOrderVo.getGermchitId());
		// 不存在该种苗信息或者库存量不足
		if (germchit == null || germchit.getGermchitAmount() < germchitOrderVo.getPurchaseAmount()) {
			return false;
		} else {
			// 创建一个订单实体，用户存放订单信息
			threadPool.submit(() -> {
				GermchitOrder order = new GermchitOrder();
				BeanUtils.copyProperties(germchitOrderVo, order);
				// 将订单信息保存在数据库中
				germchitOrderMapper.insert(order);
			});
			// 减少商家种苗的库存量
			germchit.setGermchitAmount(germchit.getGermchitAmount() - germchitOrderVo.getPurchaseAmount());
			germchitMapper.updateById(germchit);
			return true;
		}
	}


	/**
	 * 获取基地所有种苗信息
	 *
	 * @param baseId
	 * @return
	 */
	@Override
	public List<BaseGermchitVo> getAllGermchitInfo(String baseId) {
		return germchitManagementMapper.findAllBaseGermchit(baseId);
	}


	/**
	 * 获取基地所有种苗信息带分页
	 *
	 * @param baseId
	 * @return
	 */
	@Override
	public Page<BaseGermchitVo> getAllGermchitInfoWithPage(String baseId, int size, int current) {
		// 创建一个页面对象，用于保存页面的数据
		Page<BaseGermchitVo> page = new Page<>(current, size);
		// 从数据库中分页查询出数据
		Future<List<BaseGermchitVo>> future = threadPool.submit(() ->
				germchitManagementMapper.findAllBaseGermchitWithPage(baseId, size, (current - 1) * size));
		// 从数据库中获取总记录条数
		int total = germchitManagementMapper.findBaseGermchitAmount(baseId);
		// 填充总记录数
		page.setTotal(total);
		// 填充总页码数
		page.setPages(total / size + (total % size == 0 ? 0 : 1));
		// 填充数据
		try {
			page.setRecords(future.get());
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return page;
	}

	@Override
	public List<BaseGermchitAmountVo> getAmountInfoOfGermchit(String baseId) {
		return germchitManagementMapper.findAmountInfoOfBaseGermchit(baseId);
	}
}
